?><?
$url_override=uoWWW;
function get_url_override(){global $url_override;return $url_override;}
function set_email_url(){global $url_override;$url_override=uoEmail;}
function set_print_url(){global $url_override;$url_override=uoPrint;}
function set_html_url(){global $url_override;$url_override=uoWWW;}
function set_controls_url(){global $url_override;$url_override=uoControls;}
function set_presentation_url(){global $url_override;$url_override=uoPresentation;}
function locate($url){header("Location: ".$url);}

function clear_url_override(){
  global $url_override;
  global $saved_url_override;
  $saved_url_override=$url_override;
  $url_override=uoWWW;
}

function restore_url_override(){
  global $url_override;
  global $saved_url_override;
  $url_override=$saved_url_override;
}

function get_url_prefix(){
  global $url_override;
  if(isset($url_override)){
    switch($url_override){
      case uoPresentation: return "";
      case uoEmail       : return get_host();
      case uoWWW         : return get_host();
      case uoPrint       : return get_host();
      case uoControls    : return get_host();
    }
  }else{
    return "";
  }
}
function get_url_suffix(){
  global $url_override;
  if(isset($url_override)){
    switch($url_override){
    case uoPresentation: return ".html";
    case uoEmail       : return "";
    case uoWWW         : return "";
    case uoPrint       : return "print/";
    case uoControls    : return "";
    }
  }else{
    return "";
  }
}
function get_parsed_url($url){
  global $url_override;
  if(isset($url_override)){
    switch($url_override){
      case uoWWW          : return urlSign.$url;
      case uoControls     : return urlSign.$url;
      case uoPresentation : return "static".str_replace("/",".",$url);
    }
  }
  return urlSign.$url;
}
function close(){
  $obj=&get_current_page();
  std_js("grape/scripts/grape.js");
  ?><script type="text/javascript" >close_control('<?=$obj->url()?>');</script><?
}
function get_server(){
  $HTTP_SERVER_VARS=$_SERVER;
  $server = "";
  if(empty($HTTP_SERVER_VARS['HTTP_X_FORWARDED_HOST'])) {
    $server = getenv('HTTP_X_FORWARDED_HOST');
  } else {
    $server = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_HOST'];
  }

  if ( $server == "" ) {
    if (empty($HTTP_SERVER_VARS['HTTP_X_FORWARDED_SERVER'])) {
      $server = getenv('HTTP_X_FORWARDED_SERVER');
    } else {
      $server = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_SERVER'];
    }
  }
  if ( $server == "" ) {
    if (empty($HTTP_SERVER_VARS['HTTP_HOST'])) {
      $server = getenv('HTTP_HOST');
    } else {
      $server = $HTTP_SERVER_VARS['HTTP_HOST'];
    }
  }
  return $server;
}
function get_host(){
  
  global $url_override;
  if($url_override==uoPresentation)return "";

  $HTTP_SERVER_VARS=$_SERVER;
  // IIS sets HTTPS=off
  if (isset($HTTP_SERVER_VARS['HTTPS']) && $HTTP_SERVER_VARS['HTTPS'] != 'off') {
    $proto = 'https://';
  } else {
    $proto = 'http://';
  }
  
  return $proto.get_server();
}
function get_domain(){
  $host=substr(get_host(),7);
  $parts=explode(".",$host);
  $host=$parts[count($parts)-2].".".$parts[count($parts)-1];
  return $host;
}

function get_full_domain(){return substr(get_host(),7);}

function transit($replace=array()){
  global $params;
  $result="";
  foreach($params as $name=>$value){
    if(!is_array($value)){
      if($value!=""){
        $param  = array_key_exists($name,$replace)===false?$value:$replace[$name];
        $result.=($result==""?"?":"&").$name."=".urlencode($param);
        if(array_key_exists($name,$replace))unset($replace[$name]);
      }
    }elseif($name==ptControl){
       foreach($value as $param){
         $result.="/".urlencode($param);
       }
    }    
  }
  if(is_array($replace)&&count($replace)>0){
    foreach($replace as $name=>$value){
      if($value!=""){
        $result.=($result==""?"?":"&").$name."=".urlencode($replace[$name]);
      }
    }
  }
  return $result;
}

function get_param($remove=false,$index_add=0){
  global $params;
  global $params_index;
  if(!isset($params_index)){$params_index=0;}  
  if(isset($params[ptControl][$params_index+$index_add])){
    $param=$params[ptControl][$params_index+$index_add];
  }else{
    $param=false;
  }
  if($remove){
    $params_index++;
  }
  if($param=="")$param=false;
  return $param;
}


function set_404($entity="",$item=""){
  header("HTTP/1.0 404 Not Found");
  header('Status: 404');
  echo "<h1>404</h1><br>".$entity.":".$item." not found!";      

}

function valid_url($url){return preg_match('/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i' ,$url);}
function isValidURL($url){return valid_url($url);}
function get_visitor(){return session_id();}